<?php
defined('BASEPATH') OR exit('No direct script access allowed');

//会员饮食信息
class YinShi extends MY_Controller {

	function __construct(){
		parent::__construct();
		
		$this->load->model('User/UserDietsModel', 'item');
	}	

	//start
	/**
	* @name 获取会员饮食列表
	* @param type ture int 类型
	* @param uid false int 类型
	* @param page false int 页面
	* @param size false int 记录数量
	* @return 返回数组对象
	*/
	//end
	public function get_list(){		
		$user_id = $this->user_id;
		
		$page	= $this->input->post('page',null,1);
		$size	= $this->input->post('size',null,10);
		$type	= $this->input->post('type',null,0);
		$uid	= $this->input->post('uid',null,0);
		
		
		if($page<1)$page=1;
		if($size<1)$size=10;

		$arr = array(
			"status" => 0,
			"type"   => $type,
			"uid"	=> $uid 
		);
		$res = $this->item->get_list($arr,$page,$size);
		
		
		if(($page-1)*$size>$res['count']){
			$this->err_out('没有了',10);
		}
		$list = $res['list'];
		$this->load->model('User/UserDietsCommentModel', 'comment');
		$type = array('','早餐','中餐','晚餐','加餐');
		
		foreach($list as $key=>$val){
			$comment = $this->comment->get_lists($val['id'],10);
			$list[$key]['comment_list'] = $comment;
			$list[$key]['datetime'] = date("Y-m-d H:i:s",$val['addtime']);
			$list[$key]['type_name']= $type[$val['type']];
		}
		

		
		$data['result_code'] = 0;
		$data['message']     = '获取列表成功';
		$data['data'] = array('count'=>$res['count'],'list'=>$list);
	
		$this->out($data);
	}
	
	//start
	/**
	* @name 获取会员饮食详细
	* @param id ture int 饮食ID
	* @return 返回数组对象
	*/
	//end
	public function get_info(){		
		$id		= $this->input->post('id',null,'0');		
		$info = $this->item->get_info($id);
		if(!$info){
			$this->err_out('数据不存在');
		}
		
		$res = $this->item->get_info($id);
		
		$data['result_code'] = 0;
		$data['message']     = '成功';
		$data['data'] 		 = $res;
	
		$this->out($data);
	}	
	//start
	/**
	* @name 添加饮食评价
	* @param did ture int 饮食ID
	* @param info ture int 评价内容
	* @return 返回数组对象
	*/
	//end
	public function comment(){
		$user_id = $this->user_id;
		$did		= $this->input->post('did',null,0);
		$info		= $this->input->post('info',null,'');
		
		$this->load->model('User/UserDietsCommentModel', 'comment');
		
		$time = time();
		
		$arr = array(
			'cid'=>$user_id,
			'did'=>$did,
			'info'=>$info,
			'addtime'=>$time
		);
		
		$ver_arr = array(
			'did'=>"饮食ID不能为空",
			'info'=>"评价内容不能为空"
		);
		
		$result = $this->CheckData($ver_arr,$arr);
		
		if($result){
			$this->err_out($result);
		}
		
		$this->comment->add($arr);
		$this->data_out(1);
	}

	//start
	/**
	* @name 删除会员饮食和相关评论
	* @param id ture int 饮食ID
	* @return 返回数组对象
	*/
	//end
	public function del_list_comment(){		
		$id		= $this->input->post('id',null,'0');		
		
		$res = $this->item->del("id='{$id}'");
		if($res){
			$this->load->model('User/UserDietsCommentModel', 'comment');
			$this->comment->del("did='{$id}'");
			$data['result_code'] = 0;
			$data['message']     = '删除成功';
		}else{
			$data['result_code'] = 1;
			$data['message']     = '删除失败，请稍后再试！';
		}
		
		$this->out($data);
	}
}
